package cn.web.web_module.service.impl;

import cn.itcast.mapper.horsemanMapper;
import cn.itcast.mapper.orderMapper;
import cn.itcast.pojo.horseman;
import cn.itcast.pojo.order;
import cn.itcast.service.impl.orderServiceImpl;
import cn.web.web_module.service.OptimalDistanceService;
import cn.web.web_module.util.MyUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: 张松
 * @Description:    最优距离算法
 * @Date: Created in 11:59 2020/7/25
 * @Modified By:
 */
@Service
public class OptimalDistanceServiceImpl implements OptimalDistanceService {

    @Resource
    private horsemanMapper horsemanMapper;

    @Resource
    private orderMapper orderMapper;

    @Resource
    private orderServiceImpl orderServicelmpl;


    /**
     *
     * @Description: 根据骑手id获取最优路线
     *
     * @auther: 张松
     * @date: 16:43 2020/7/25
     * @param: [openId]
     * @return: java.util.Map
     *
     */
    @Override
    public Map getOptimalDistance(String openId) {
        horseman horseman = getHorseman(openId);
        List<order> horsemanInOrderLatAndLon = getHorsemanInOrderLatAndLon(openId);
        getCalculatesOptimal(horseman,horsemanInOrderLatAndLon);
        return null;
    }





    /**
     *
     * @Description:  根据骑手openId查找骑手
     *
     * @auther: 张松
     * @date: 15:53 2020/7/25
     * @param: [openId]
     * @return: cn.itcast.pojo.horseman
     *
     */
    @Override
    public horseman getHorseman(String openId) {
        List<horseman> horsemen = horsemanMapper.selectHorsemanOpenId(openId);
        return horsemen.get(0);
    }


    /**
     *
     * @Description: 根据骑手Id查找骑手正在进行中的订单
     *
     * @auther: 张松
     * @date: 15:53 2020/7/25
     * @param: [openId]
     * @return: java.util.List<cn.itcast.pojo.order>
     *
     */
    @Override
    public List<order> getHorsemanInOrderLatAndLon(String openId) {
        List<order> orders = orderMapper.selectHorsemanInOrder(openId);
        orders.forEach((v)->{
            if (Strings.isBlank(v.getLat())||v.getLat().equals("0")||Strings.isBlank(v.getLon())||v.getLon().equals("0")){
                Map<String, Double> lngAndLat = MyUtil.getLngAndLat(v.getUserArea());
                v.setLon(String.valueOf(lngAndLat.get("lng")));
                v.setLat(String.valueOf(lngAndLat.get("lat")));
            }
        });
        return orders;
    }


    /**
     *
     * @Description:  根据骑手经度,纬度和用户下单经度,纬度计算最优距离
     *
     * @auther: 张松
     * @date: 16:42 2020/7/25
     * @param: [horseman, orders]
     * @return: void
     *
     */
    @Override
    public void getCalculatesOptimal(horseman horseman, List<order> orders) {
        horseman.getHorsemanLat();
        horseman.getHorsemanLon();

    }

}
